var ns, app, dialog, toast;
app = new Vue({
    el: '#app',
    data: {
        list: [],
        form: {
            ID:0,
            pageindex: 0,
            pagesize: 10,
            servicetype: 1, //0-未选择 1-任务工单保修 2-投诉建议
            status: 1, //0-全部 1-进行中 2-我发起的 3-我参与的 4-已完成
            RoomID: 0,
            isviewdetail: true,
            OrderByType:0,//时间排序类型，1-由近到远；2-由远到近
            onlybaoshi: 0,
            IsWorkOrder:1,
            isFirstLoad: 1,
            statusListIDS:[],//状态多选
        },
        
        current_item_length: 0,
        is_searching: false,
        scroll_top: 0,
        can_scroll: false,
        showsearchmonth: false,
        showsearchparam: false,
        operation: {
            canVisit: false,
            canClose: false,
            canSend: false,
            canAccept: false,
            canAfreshSend: false,
            canChuidan: false,
            canRefuse: false,
            canAcceptOrder: false,
            canComplete: false,
            canHandle: false
        },
        showsearch:false,
        showTop:false,
        searchlist:[],
        choosetaskmodel:{},
        optionTitle:'',
        choosesortmodel:{},
        option2: [],
        option3: [
            // { text: '排序', value: 0 },
            { text: '按任务发布时间近到远', value: 1 },
            { text: '按任务发布时间远到近', value: 2 },
        ],
        statusList:[
            {"ID":"4","Name":"待受理"},
            {"ID":"5","Name":"待派单"},
            {"ID":"10","Name":"待接单"},
            {"ID":"0","Name":"处理中"},
        ],
        number:0,
        servicetypename:'',//工单类型
        servicetypeid:'',//工单类型id
        chooseId:0,//选择的工单id
        //控制新的筛选
        showsearch: false,
        showsearchmonth: false,
        showsearchparam: false,
    },
    methods: {
        doReset: function(type) {
            var that = this;
            if (type == 2) {
                that.choosesortmodel=that.option3[0];
                that.form.OrderByType=that.option3[0].value;                                     
            } else {
                that.form.statusListIDS=[];
            }

        },
        doSearch: function() {
            var that = this;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.doHideSearch();
            that.get_data();
        },
        doSelectSearchParam: function(type) {
            var that = this;
            if (type == 1) { //筛选
                that.showsearchparam = !that.showsearchparam;
                that.showsearchmonth = false;
            }
            if (type == 2) { //排序
                that.showsearchparam = false;
                that.showsearchmonth = !that.showsearchmonth;
            }
            that.showsearch = that.showsearchparam || that.showsearchmonth;     
        },
        get_data: function () {
            var that = this;
            that.is_searching = true;
            that.can_scroll = false;
            // that.searchlist=[];
            var options = {};
            options.pagesize = that.form.pagesize;
            options.pageindex = that.form.pageindex;
            options.servicetype = that.form.servicetype;
            options.OrderByType = that.form.OrderByType;
            options.action = "getservicetaskhistory";
            options.Status = that.form.status;
            options.isAnalysis = that.form.isviewdetail ? 1 : 0;
            options.isFirstLoad = that.form.isFirstLoad; 
            options.ServiceStatus = JSON.stringify(that.form.statusListIDS);
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.list.length == that.form.pagesize) {
                        that.can_scroll = true;
                    }
                    if (that.form.pageindex == 0) {
                        that.searchlist = data.list;
                    } else {
                        that.searchlist = that.searchlist.concat(data.list);
                    }
                    app.current_item_length = that.searchlist.length;
                    that.form.isFirstLoad = 0;
                    for(var i=0;i<data.list.length;i++){
                        data.list[i].AddTime=ns.SetTime_style(data.list[i].AddTime);
                    }
                    
                } else if (err) {
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });
        },
        //分类
        get_menuType:function(){
            var that=this;

            //状态默认选中第一个
            that.choosetaskmodel=that.statusList[0];
            //that.doSelectstatus(that.choosetaskmodel.ID);
             //排序默认选中第一个
            that.choosesortmodel=that.option3[0];
            that.form.OrderByType=that.option3[0].value;    

            //以前的
            // var options = {};
            // options.servicetype = that.form.servicetype;
            // options.action = 'getservicetaskhistorysearchparams';
            // ns.post(options, function (succeed, data, err) {
            //     if(succeed){
            //         if(data.status){
            //             that.option2=data.list;
            //             that.choosetaskmodel=data.list[1];
            //             that.optionTitle=that.choosetaskmodel.Name+'('+that.choosetaskmodel.Number+')';
            //             that.choosesortmodel=that.option3[0];
            //         }
            //     }else{
            //         api.toast({
            //             msg: err,
            //             duration: 2000,
            //             location: 'bottom'
            //         });
            //     }
            // })

        },
        doSelectstatus:function(ID){
            var that = this;
            var index = that.form.statusListIDS.indexOf(ID);
            if (index > -1) {
                that.form.statusListIDS.splice(index, 1);
            } else {
                that.form.statusListIDS.push(ID);
            }
        },
        //翻页第一条数据的位置
        do_scrollbtn:function(id){
    　　　　 var sollid = document.getElementById('scroll'+id);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
    　　},
        //选择分类
        onChooseTask:function(item){
            var that = this;
            that.choosetaskmodel=item;
            that.optionTitle=that.choosetaskmodel.Name+'('+that.choosetaskmodel.Number+')';
            this.$refs.choosetask.toggle();
            that.form.status=item.Type;
            that.get_data();
        },
        //选择排序
        onChooseSort:function(item){
            var that = this;
            that.choosesortmodel=item;
            // this.$refs.choosesort.toggle();
            that.form.OrderByType=item.value;
            that.form.pageindex = 0;
            that.form.pagesize = 10;
            that.doHideSearch();
            that.get_data();
        },

        get_operation: function () {
            var that = this;
            var options = {};
            options.action = "getserviceoperation";
            options.servicetype = that.form.servicetype;
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    that.operation = data.operation;
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        // select_status: function (status) {
        //     var that = this;
        //     that.showTop=false;
        //     that.form.status = status;
        //     that.list = [];
        //     that.get_data();
        // },
        openGongdan: function (item) {
            var that = this;
            ns.openWin('home_task_con_frm', '任务', {
                id: item.ID,
                isviewdetail: that.form.isviewdetail
            });
        },
        doOpenServiceProcess: function (type) {
            ns.openWin('analysis_customerservice_process_frm', '工单进度');
        },
        //判断是否受理
        do_isShouli:function(item){
            var that=this;
            that.chooseId=item.ID;
            if(!item.ServiceTypeIsDeep){
                ns.openWin('neibubaoshi_typelist', '工单类型选择',{
                    placetype:1,//1-微信公众号改工单类型
                    gongdanType:that.form.servicetype
                });
            }else{
                that.do_open_shouli();
            }
        },
        //修改工单类型
        do_editType(){
            var that = this;
            var options={};
            options.ID=that.chooseId;
            options.TypeID=that.servicetypeid;
            options.TypeName=that.servicetypename;
            options.action="editcustomerservice";
            ns.post(options, function (succeed, data, err) {
                if(succeed){
                    if(that.chooseId!=0){
                        that.do_open_shouli();
                    }
                    
                }else{
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            })

        },
        // 受理
        do_open_shouli: function () {
            var that = this;
            var options = {};
            options.ID = that.chooseId;
            options.action = "customerserviceshouli";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true) {
                    that.form.isFirstLoad = 1;
                    // that.select_status(5);
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else {
                    api.toast({
                        msg: data.Msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.isFirstLoad = 1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        // 派单
        do_open_chooseuser: function (item, type) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = 'getcustomerservicestatus';
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('chooseserviceuser_frm', '派单处理', {
                        source: 'transfer',
                        id: item.ID,
                        type: type,
                        ServiceType: that.form.servicetype
                    });

                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.isFirstLoad = 1;
                    that.get_data();

                }


            });
        },
        //催单
        do_open_cuidan: function (id) {
            var that = this;
            var options = {};
            options.ID = id;
            options.action = "jpushuser";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    toast.custom({
                        title: "催单成功",
                        html: '<i class="icon iconfont icon-yidu"></i>',
                        duration: 2000
                    });

                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //拒绝
        do_open_nodialog: function (id) {
            var that = this;
            dialog.alert({
                title: "提示",
                msg: '拒绝接受该工单，工单将流转至派单人。是否确认拒绝？',
                buttons: ['取消', '确认拒绝']
            }, function (ret) {
                if (ret.buttonIndex == 2) {
                    var options = {};
                    options.ID = id;
                    options.action = "refuseservice";
                    ns.post(options, function (succeed, data, err) {
                        if (data.status == true) {
                            that.form.isFirstLoad = 1;
                            that.get_data();
                        } else {
                            api.toast({
                                msg: data.Msg,
                                duration: 2000,
                                location: 'bottom'
                            });
                            that.form.isFirstLoad = 1;
                            that.get_data();
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });

                }
            })

        },
        //关单
        do_open_dialog: function (id) {
            var that = this;
            dialog.alert({
                title: "提示",
                msg: '工单关闭后，将不能进行任何操作，请确认工单是否已经处理完成？',
                buttons: ['取消', '确认关闭']
            }, function (ret) {
                if (ret.buttonIndex == 2) {
                    var options = {};
                    options.ID = id;
                    options.action = "serviceclose";
                    ns.post(options, function (succeed, data, err) {
                        if (succeed) {
                            that.form.isFirstLoad = 1;
                            that.get_data();
                            api.sendEvent({
                                name: 'reload_Analysis',
                            });
                        } else if (err) {
                            api.toast({
                                msg: err,
                                duration: 2000,
                                location: 'bottom'
                            });
                        }
                    }, {
                        toast: true,
                        toastmsg: '提交中'
                    });

                }
            })
        },
        //接单
        do_accpet: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "acceptservice";
            ns.post(options, function (succeed, data, err) {
                if (succeed) {
                    if (data.status == true) {
                        that.form.isFirstLoad = 1;
                        // that.select_status(0);
                        // that.get_data();
                        api.sendEvent({
                            name: 'reloadservicelist',
                        });
                    } else {
                        api.toast({
                            msg: '状态已改变，请重新选择',
                            duration: 2000,
                            location: 'bottom'
                        });
                        that.form.isFirstLoad = 1;
                        that.get_data();
                    }
                }else if (err){
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }

            }, {
                toast: true,
                toastmsg: '提交中'
            });
        },
        //完工
        do_complete: function (item) {
            var that = this;
            var options = {};
            options.ID = item.ID;
            options.action = "getcustomerservicestatus";
            ns.post(options, function (succeed, data, err) {
                if (data.status == true && data.ServiceStatus == item.ServiceStatus) {
                    ns.openWin('renwugongdancomplete_frm', '完工记录', {
                        source: 'transfer',
                        id: item.ID,
                        type: 2,
                        ServiceType: item.ServiceType
                    });
                } else {
                    api.toast({
                        msg: '状态已改变，请重新选择',
                        duration: 2000,
                        location: 'bottom'
                    });
                    that.form.isFirstLoad = 1;
                    that.get_data();
                }
            }, {
                toast: true,
                toastmsg: '提交中'
            });

        },
        //处理
        do_chuli: function (item) {
            var that = this;
            ns.openWin('renwugongdancomplete_frm', '处理记录', {
                source: 'transfer',
                id: item.ID,
                type: 1,
                ServiceType: item.ServiceType
            });
        },
        //回访
        do_huifang: function (item) {
            var that = this;
            ns.openWin('renwugongdanreturnvisit', '回访记录', {
                source: 'transfer',
                id: item.ID,
                type: 3,
                ServiceType: item.ServiceType
            });
        },
        open_select:function(){
            var that=this;
            that.showTop=!that.showTop;
        },
        doHideSearch:function(){
            var that=this;
            that.showTop=false;
            that.showsearch=false;

            that.showsearchparam = false;
            that.showsearchmonth = false;
            that.showsearch = false;
        },
        select_allstatus:function(){
            var that=this;
            that.showsearch=!that.showsearch;
            
            var options={};
            options.servicetype = that.form.servicetype;//状态筛选，支持多选和单选
            options.action = "getservicehistory";
            ns.post(options, function(succeed, data, err) {
                if (succeed) {
                    that.searchlist = data.list;
                    
                } else if (err) {
                    that.list = [];
                    that.current_item_length = 0;
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                toast: true
            });

        },
        //搜索结果返回位置
        do_scrollTo:function(){
            var that = this;
    　　　　 var sollid = document.getElementById('scroll'+that.form.ID);
    　　　　 window.scrollTo(0,sollid.offsetTop); 
            var classlist=document.getElementById('scroll'+that.form.ID).getElementsByClassName('boxbg')[0];
            classlist.style.backgroundColor = "#eaf1ff";
            setTimeout(function() {
                classlist.style.backgroundColor = "#f7fbff";
            }, 800)
            
    　　},


    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    dialog = new auiDialog();
    toast = new auiToast();
    app.form.servicetype = api.pageParam.servicetype || 1;
    app.form.RoomID = api.pageParam.id || 0;
    app.form.isviewdetail = api.pageParam.isviewdetail || false;
    app.form.IsWorkOrder=api.pageParam.IsWorkOrder || 1;
    app.form.ID = api.pageParam.ID || 0;
    app.get_menuType();

    if (app.form.isviewdetail) {
        app.form.status = 4;
    }
    app.form.onlybaoshi = api.pageParam.onlybaoshi || 0;
    api.addEventListener({
        name: 'reloadservicelist'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'customerservicelist_frm'
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    api.addEventListener({
        name: 'is_hidden_right',
    }, function (ret, err) {
        app.form.isFirstLoad = 1;
        app.get_data();
    });
    // ns.init_pull(true,true);
    ns.init_pull();
    app.get_data();
    setTimeout(function () {
        app.get_operation();
    }, 500)
    api.addEventListener({
        name: 'dosaveservicetype'
    }, function (ret) {
        app.servicetypename = ret.value.item.text;
        app.servicetypeid = ret.value.item.id;
        app.do_editType();

    })

    // api.addEventListener({
    //     name: 'do_open_select'
    // }, function(ret, err) {
    //     app.open_select();
    // });
};
